#include "Bullet.h"

using namespace Ogre;
using namespace NxOgre;

int Bullet::bulletID = 0;

Bullet::Bullet(NxOgre::Scene* nxScene, Player* player)
{
	// Meta Data
	bulletName = "bullet" + Ogre::StringConverter::toString(bulletID);
	bulletID++;
	bulletFirer = player;
	lifeTime = 0.0f; // how long has it been around

	// Set up bullet properties
	dir = player->getOrientation() * Vector3::UNIT_Z;
	startPos = Ogre::Vector3(0,20,0) + player->getPosition() + dir*10;
	rayStartPos = Ogre::Vector3(0,20,0) + player->getPosition() + dir*18;
	//startPos = player->getPosition() + dir*5;
	//rayStartPos = player->getPosition() + dir*13;
	range = 1000;
	collisionObj = "";
	_hasCollided = false;
	_collidePlayer = false;
	_collideTerrain = false;

	// Create the bullet
	bulletBody = nxScene->createBody(bulletName+";Bullet.mesh", new CubeShape(4,4,8), startPos, "mass: 1, node: "+bulletName);
	//bulletBody = nxScene->createBody(bulletName+";9mmPellet.mesh", new CubeShape(4,4,8), startPos, "mass: 1, node: "+bulletName);
	bulletSceneNode = bulletBody->getNode();
	bulletEntity = bulletBody->getEntity();

	// Check for collisions using NxRayCasting
	/*NxOgre::Actor* actor = 0;
	//NxOgre::Character* charac = 0;
	NxOgre::RayCaster caster = NxOgre::RayCaster(rayStartPos, dir, range, NxOgre::RayCaster::RCT_CLOSEST, nxScene);
    if( caster.castShape(NxOgre::RayCaster::AF_NONE) )
	{
		actor = caster.getClosestActor();
	//	charac = caster.getClosestCharacter();
		if (actor)
		{
			NxOgre::Body* temp = static_cast<NxOgre::Body*>(actor);
			collisionObj = temp->getEntity()->getName();
			_collideTerrain = true;
			range = caster.getClosestRaycastHit().mRaycastHit.distance;
		}

		//if (charac)
		//{
		//	collisionObj = charac->getEntity()->getName();
		//	_collidePlayer = true;
		//	range = caster.getClosestRaycastHit().mRaycastHit.distance;
		//}
	}*/

	bulletBody->setGlobalOrientation(player->getOrientation());	
	bulletBody->addForce(dir*20000);
}

bool Bullet::updateBullet(NxOgre::Scene* nxScene, Player* p1, Player* p2, const FrameEvent& evt)
{
	lifeTime+=evt.timeSinceLastFrame;
	Ogre::Vector3 currPos = bulletBody->getGlobalPosition();
	
	Player* victim = 0;
	if (p1 == bulletFirer) victim = p2;
	if (p2 == bulletFirer) victim = p1;

	Ogre::Real dist = currPos.distance(victim->getPosition());
	
	// chk for player hit
	if (dist < 25.0 && lifeTime < 1.0f)
	{
		nxScene->destroyBody(bulletName);
		bulletBody = 0;
		_hasCollided = true;
		if (dist > 20.0) victim->health -= 15;
		else victim->health -= 20;
		_collidePlayer = true;
		return true;
	}
	else if (currPos.distance(startPos) > range) // move off range
	{
		nxScene->destroyBody(bulletName);
		bulletBody = 0;
		return true;
	}

	//else if (currPos.distance(startPos) > range)
	//{
	//	nxScene->destroyBody(bulletName);
	//	bulletBody = 0;
	//	_hasCollided = true;
	//	_collideTerrain = true;
	//	return true;
	//}
	else return false;
	
	/*Ogre::Vector3 currPos = bulletBody->getGlobalPosition();
	if (currPos.distance(startPos) > range)
	{
		nxScene->destroyBody(bulletName);
		bulletBody = 0;
		_hasCollided = true;
		return true;
	}
	return false;*/
}